package com.android.music.prolyric.search;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.widget.TextView;

public class DataHandler extends DefaultHandler
{
	
		private boolean bTitle = false;
		private boolean bSinger = false;
		private boolean bSource = false;	
		
		String[] str;
		
	//constructor
	public DataHandler() {
	    super();	   	    
	    
	    str = new String[3];
	}
	
	//start of the XML document
		public void startDocument () { 
			//Log.i("DataHandler", "Start of XML document"); 
		}

		//end of the XML document
		public void endDocument () { 
			//Log.i("DataHandler", "End of XML document"); 
		}

		//opening element tag
		public void startElement (String uri, String name, String qName, Attributes atts)
		{
		    //handle the start of an element
			
			if (qName.equalsIgnoreCase("title")) {
    			bTitle = true;
    		}
     
    		if (qName.equalsIgnoreCase("singer")) {
    			bSinger = true;
    		}
     
    		if (qName.equalsIgnoreCase("source")) {
    			bSource = true;
    		}  
		}

		//closing element tag
		public void endElement (String uri, String name, String qName)
		{
		    //handle the end of an element
			//Log.i("End Element :",qName);
		}

		//element content
		public void characters (char ch[], int start, int length)
		{
			if (bTitle) {    			
    			
    			str[0]=new String(ch, start, length);
    			bTitle = false;
    		}
     
    		if (bSinger) {
    			
    			str[1]=new String(ch, start, length);
    			bSinger = false;
    		}
     
    		if (bSource) {
    			
    			str[2]=new String(ch, start, length);
    			bSource = false;
    		}
		}
		
		public String[] getData(String url)
		{
		    //take care of SAX, input and parsing errors
		    try
		    {
		            //set the parsing driver
		        System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");
		            //create a parser
		        SAXParserFactory parseFactory = SAXParserFactory.newInstance();
		        SAXParser xmlParser = parseFactory.newSAXParser();
		            //get an XML reader
		        XMLReader xmlIn = xmlParser.getXMLReader();
		            //instruct the app to use this object as the handler
		        xmlIn.setContentHandler(this);
		            //provide the name and location of the XML file **ALTER THIS FOR YOUR FILE**
		        URL xmlURL = new URL(url);
		            //open the connection and get an input stream
		        URLConnection xmlConn = xmlURL.openConnection();
		        InputStreamReader xmlStream = new InputStreamReader(xmlConn.getInputStream());
		            //build a buffered reader
		        BufferedReader xmlBuff = new BufferedReader(xmlStream);
		            //parse the data
		        xmlIn.parse(new InputSource(xmlBuff));
		        return str;
		    }
		    
		    catch(SAXException se) { Log.e("AndroidTestsActivity", 
		            "SAX Error " + se.getMessage()); }
		    catch(IOException ie) { Log.e("AndroidTestsActivity", 
		            "Input Error " + ie.getMessage()); }
		    catch(Exception oe) { Log.e("AndroidTestsActivity", 
		            "Unspecified Error " + oe.getMessage()); }
		    return null;
		   
		}

}
